define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val(),
                    cate_id = $('#cate_id').val(),
                    order = $('.selected').data('type');
                var page = 1,flag = true,goods_name,goods_id;

                getList();

                /*搜索*/
                $('.search-btn').click(function(){
                    page = 1;
                    goods_name = $('#search').val();
                    $('#goodlist').html('');
                    getList();
                });

                $('#order a').click(function(){
                    page = 1;
                    $('#goodlist').html('');
                    $(this).addClass('selected').siblings().removeClass('selected');
                    if($(this).hasClass('orderbyprice')){
                        if($(this).hasClass('up')){
                            $(this).addClass('down').removeClass('up');
                            $('.triangle-up').addClass('triangle-check');
                            $('.triangle-down').removeClass('triangle-check');
                        }else{
                            $(this).addClass('up').removeClass('down');
                            $('.triangle-down').addClass('triangle-check');
                            $('.triangle-up').removeClass('triangle-check');
                        }
                        order = $('.triangle-check').data('type');
                    }else{
                        order = $(this).data('type');
                    }
                    getList();
                })

                /*滑动加载*/
                $('.container').scroll(function () {
                    // console.log($('.contain').outerHeight());
                    // console.log($('.contain').scrollTop());
                    console.log($('.container').scrollTop() + $('.container').outerHeight()+1);
                    console.log($('.container-child').outerHeight());
                    if (($('.container').scrollTop() + $('.container').outerHeight() + 1) >= ($('.container-child').outerHeight())) {
                        if (flag) {
                            getList();
                        }
                    }
                });

                $(document).on('click','.addcart',function(){
                    goods_id = $(this).data('goods_id');
                    cart();
                })
                /*$(document).on('click','.change-num-btn',function(){
                    goods_id = $(this).data('goods_id');
                    var status = $(this).data('status');
                    var num = $(this).siblings('.cart-num').html();

                    if(status == 'plus'){
                        if(cart('/restaurant/TrustworthyShop/addCarts')){
                            if(num == 0){
                                num ++;
                                $(this).siblings('.icon-jian').removeClass('hide');
                                $('.cart-num-'+ goods_id).html(num).removeClass('hide');
                            }else{
                                num ++;
                            }
                        }
                    }else{
                        if(cart('/restaurant/TrustworthyShop/delCarts')){
                            num --;
                            if(num == 0){
                                $(this).addClass('hide');
                                $('.cart-num-'+ goods_id).html(num).addClass('hide');
                            }
                        }
                    }
                    $('.cart-num-'+ goods_id).html(num);

                })*/

                function getList(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/goodList',
                        type: 'post',
                        data: {store_id:store_id,p:page,goods_name:goods_name,order:order,cate_id:cate_id},
                        dataType: 'json',
                        beforeSend: function(){
                            flag = false;
                            $('.no_more').remove();
                            $('.circle-loadding').show();
                        },
                        success: function(data){
                            if(data.status == 'S') {
                                if(data.cartCount > 0){
                                    $('.tips').html(data.cartCount).removeClass('hide');
                                }

                                var goods_list = '',no_more = '';
                                if(data.goods.length > 0){
                                    $.each(data.goods, function (i, k) {
                                        goods_list += '<div class="item list-item gc-flex gc-flex-lc">' +
                                            '<a href="/restaurant/TrustworthyShop/gooddetail/store_id/' + store_id + '/goods_id/' + k.goods_id + '" class="item-img">' +
                                            '<img src="' + k.goods_thumb + '"></a><div class="item-msg gc-flex-item">' +
                                            '<a href="/restaurant/TrustworthyShop/gooddetail/store_id/' + store_id + '/goods_id/' + k.goods_id + '" class="item-name gc-ft-normal">' + k.goods_name + '</a>' +
                                            '<div class="gc-flex gc-flex-lc"><span class="gc-ft-red price">￥' + k.goods_price + '</span><span class="gc-ft-sm gc-ft-gray">已售' + k.sale_num + '</span></div>' +
                                            '</div><a href="javascript:;" class="iconfont icon-icon gc-ft-deepgray addcart" data-goods_id="' + k.goods_id + '"></a></div>';
                                    });
                                    if(data.goods.length < 6){
                                        flag = false;
                                        no_more += '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">没有更多商品咯~</p>';
                                        $('#goodlist').after(no_more);
                                    }else{
                                        page ++;
                                        flag = true;
                                    }
                                    $('#goodlist').append(goods_list);
                                }else{
                                    if(page == 1){
                                        html = '<div class="full"><div class="empty"><img src="/assets/restaurant/TrustworthyShop/images/empty2.png"/></div>' +
                                            '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">暂无符合该条件的商品</p></div>';
                                        $('#goodlist').html(html);
                                    }
                                    flag = false;
                                }
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

                function cart() {
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/addCarts',
                        type: 'post',
                        data: {store_id: store_id,goods_id:goods_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                var num = $('.tips').html();
                                num ++;
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                $('.tips').html(num).removeClass('hide');
                            }else{
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

               /* $.ajax({
                    url: '',
                    type: 'post',
                    data: {store_id:store_id},
                    dataType: 'json',
                    beforeSend: function(){
                        $('.circle-loadding').show();
                    },
                    success: function(data){
                        $('.circle-loadding').hide();
                    }
                })*/
            });
        });
    };
});
